package com.cookie.meetproject.ui;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cookie.framework.base.BaseBackActivity;
import com.cookie.framework.bmob.BmobManger;
import com.cookie.framework.bmob.SquareSet;
import com.cookie.framework.helper.FileHelper;
import com.cookie.framework.view.LoadingView;
import com.cookie.meetproject.R;

import java.io.File;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.meetproject.ui
 * 文件名:    PushActivity
 * 创建者:    cookie
 * 创建时间:   2020/5/30 18:03
 * 描述:      发布广场
 */

public class PushSquareActivity extends BaseBackActivity implements View.OnClickListener {

    private EditText etContent;
    private TextView tvContentSize;
    private ImageView ivError;
    private TextView tvMediaPath;
    private LinearLayout llMedia;
    private LinearLayout llCamera;
    private LinearLayout llAblum;
    private LinearLayout llMusic;
    private LinearLayout llVideo;
    private LinearLayout llMediaType;

    //要上传的文件
    private File uploadFile = null;
    //媒体类型
    private int MediaType = SquareSet.PUSH_TEXT;
    private LoadingView mLoadingView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_push_square);
        initView();
    }

    private void initView() {

        mLoadingView = new LoadingView(this);
        mLoadingView.setLoadingText(getString(R.string.text_push_ing));

        etContent = (EditText) findViewById(R.id.et_content);
        tvContentSize = (TextView) findViewById(R.id.tv_content_size);
        ivError = (ImageView) findViewById(R.id.iv_error);
        tvMediaPath = (TextView) findViewById(R.id.tv_media_path);
        llMedia = (LinearLayout) findViewById(R.id.ll_media);
        llCamera = (LinearLayout) findViewById(R.id.ll_camera);
        llAblum = (LinearLayout) findViewById(R.id.ll_ablum);
        llMusic = (LinearLayout) findViewById(R.id.ll_music);
        llVideo = (LinearLayout) findViewById(R.id.ll_video);
        llMediaType = (LinearLayout) findViewById(R.id.ll_media_type);

        ivError.setOnClickListener(this);
        llCamera.setOnClickListener(this);
        llAblum.setOnClickListener(this);
        llMusic.setOnClickListener(this);
        llVideo.setOnClickListener(this);

        //输入框监听
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvContentSize.setText(s.length() + "/140");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_error:
                llMediaType.setVisibility(View.VISIBLE);
                llMedia.setVisibility(View.GONE);
                uploadFile = null;
                MediaType = SquareSet.PUSH_TEXT;
                break;
            case R.id.ll_camera:
                FileHelper.getInstance().toCamera(this);
                break;
            case R.id.ll_ablum:
                FileHelper.getInstance().toAlbum(this);
                break;
            case R.id.ll_music:
                FileHelper.getInstance().toMusic(this);
                break;
            case R.id.ll_video:
                FileHelper.getInstance().toVideo(this);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case FileHelper.CAMEAR_REQUEST_CODE:
                    uploadFile = FileHelper.getInstance().getTempFile();
                    if (uploadFile!=null){
                        llMediaType.setVisibility(View.GONE);
                        llMedia.setVisibility(View.VISIBLE);
                        tvMediaPath.setText(getString(R.string.text_push_type_img));
                        MediaType = SquareSet.PUSH_IMAGE;
                    }
                    break;
                case FileHelper.ALBUM_REQUEST_CODE:
                case FileHelper.MUSIC_REQUEST_CODE:
                case FileHelper.VIDEO_REQUEST_CODE:
                    if (data != null) {
                        Uri uri = data.getData();
                        String path = FileHelper.getInstance().getRealPathFromURI(PushSquareActivity.this, uri);
                        if (!TextUtils.isEmpty(path)) {
                            if (path.endsWith(".jpg") || path.endsWith(".png") || path.endsWith(".jpeg")) {
                                //图片
                                tvMediaPath.setText(getString(R.string.text_push_type_img));
                                MediaType = SquareSet.PUSH_IMAGE;
                            } else if (path.endsWith("mp3")) {
                                //音乐
                                tvMediaPath.setText(getString(R.string.text_push_type_music));
                                MediaType = SquareSet.PUSH_MUSIC;
                            } else if (path.endsWith("mp4") || path.endsWith("wav") || path.endsWith("avi")) {
                                //视频
                                tvMediaPath.setText(getString(R.string.text_push_type_video));
                                MediaType = SquareSet.PUSH_VIDEO;
                            }
                            uploadFile = new File(path);
                            llMediaType.setVisibility(View.GONE);
                            llMedia.setVisibility(View.VISIBLE);
                        }
                    }
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.input_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_input:
                mLoadingView.show();
                String content = etContent.getText().toString().trim();
                if (uploadFile != null) {
                    //上传文件
                    BmobFile bmobFile = new BmobFile(uploadFile);
                    bmobFile.uploadblock(new UploadFileListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                push(content, bmobFile.getFileUrl());
                            }
                        }
                    });
                } else {
                    //如果是文本类型则不能为空
                    if (!TextUtils.isEmpty(content)) {
                        push(content, "");
                    } else {
                        mLoadingView.hide();
                    }
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    //发表
    private void push(String content, String path) {
        BmobManger.getInstance().pushSquare(MediaType, content, path, new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                mLoadingView.hide();
                if (e == null) {
                    setResult(Activity.RESULT_OK);
                    finish();
                } else {
                    Toast.makeText(PushSquareActivity.this, getString(R.string.text_push_fail) + e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
